﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Actuals.CoreDomain.Business;
using Actuals.Business;
using Actuals.CoreDomain;

namespace Actuals
{
    public partial class AddSprint : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            Response.AddHeader("Cache-control", "no-store, must-revalidate,private,no-cache");
            Response.AddHeader("Pragma", "no-cache");
            Response.AddHeader("Expires", "0");

            Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);

            if (!Page.IsPostBack)
            {
                IBusinessProject businessProject = new BusinessProject();
                IList<IProject> projects = businessProject.GetProjects();
                ddlProject.DataSource = projects;
                ddlProject.DataTextField = "projectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind();
                ListItem select = new ListItem();
                select.Value = "0";
                select.Text = "Select";
                ddlProject.Items.Insert(0, select);


                if (Request.QueryString["Mode"] != null)
                {

                    string _mode = Request.QueryString["Mode"].ToString();
                    ViewState["Mode"] = _mode;
                    string _sprintId = Request.QueryString["SprintId"].ToString();

                    hidSprintId.Value = _sprintId;
                    PopulateSprint(int.Parse(_sprintId));
                }
                else
                {
                    string _projectId = string.Empty;
                    string _releaseId = string.Empty;
                    //_projectId = Session["ProjectId"].ToString();
                    //_releaseId = Session["ReleaseId"].ToString();

                    //ddlProject.SelectedValue = _projectId;
                    //ddlProject_OnSelectedIndexChanged(null, null);
                    //ddlRelease.SelectedValue = _releaseId;

                }

            }



        }


        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId));
            ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";
            ddlRelease.Items.Insert(0, select);
        }

        protected void ddlProject_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);
        }
        protected void PopulateSprint(int sprintId)
        {

            IBusinessSprint businessSprint = new BusinessSprint();
            ISprint sprint = businessSprint.GetSprint(sprintId);

            string _projectId = string.Empty;
            string _releaseId = string.Empty;
            if (ViewState["Mode"] == null)
            {

            }
            else
            {
                _projectId = sprint.ProjectId.ToString();
                _releaseId = sprint.ReleaseId.ToString();
            }

            ddlProject.SelectedValue = _projectId;
            ddlProject_OnSelectedIndexChanged(null, null);
            ddlRelease.SelectedValue = _releaseId;
            txtSprintDescription.Text = sprint.SprintDescription;
            txtFromDate.Text = sprint.SprintStartDate.ToString("MM-dd-yyyy");
            txtToDate.Text = sprint.SprintEndDate.ToString("MM-dd-yyyy");
            ddlstatus.SelectedValue = sprint.Status.ToString();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            ISprint sprint = new Sprint();
            sprint.SprintDescription = txtSprintDescription.Text;

            string[] _date = txtFromDate.Text.Split('-');

            sprint.SprintStartDate = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));

            string[] _toDate = txtToDate.Text.Split('-');

            sprint.SprintEndDate = new DateTime(int.Parse(_toDate[2]), int.Parse(_toDate[0]), int.Parse(_toDate[1]));


            sprint.Status = int.Parse(ddlstatus.SelectedValue);
            sprint.ProjectId = int.Parse(ddlProject.SelectedValue);
            sprint.ReleaseId = int.Parse(ddlRelease.SelectedValue);

            IBusinessSprint businessSprint = new BusinessSprint();

            if (ViewState["Mode"] != null)
            {
                sprint.SprintId = int.Parse(hidSprintId.Value);

            }

            businessSprint.AddSprint(sprint);

            lblMsg.Text = "Updated!";
            //businessSprint.S

        }

    }
}